<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2021/6/29
 * Time: 10:56
 */

namespace app\api\validate;

use app\common\model\ChangeFunction;

class Change extends Base
{
    protected $rule = [
        'companyId' => 'require|integer|min:1|enterpriseExists',
        'changCategory' => 'require|checkChangeCategory|checkChangeFunction',
    ];

    protected $message = [
        'companyId.enterpriseExists' => '公司信息不存在',
        'changCategory.checkChangeCategory' => '变更分类不存在或必填项为空',
        'changCategory.checkChangeFunction' => '暂不支持该变更',
    ];

    /**
     * 公司信息是否存在
     * @param $value
     * @return bool
     * @throws \think\Exception
     */
    protected function enterpriseExists($value)
    {
        if (!db('enterprise')->where("id={$value}")->count()) return false;
        return true;
    }

    /**
     * 变更分类不存在或必填项为空
     * @param $value
     * @return bool
     * @throws \think\Exception
     */
    protected function checkChangeCategory($value)
    {
        foreach ($value as $v) {
            if (!db('change_category')->where("id={$v['id']} and status='0'")->count()) return false;
            // if (empty($v['value'])) return false;
        }
        return true;
    }

    /**
     * 对应变更功能是否存在
     * @param $value
     * @return bool
     */
    protected function checkChangeFunction($value)
    {
        $changeCategoryIdsArr = array_column($value, 'id');
        sort($changeCategoryIdsArr);
        $changeCategoryIds = implode(',', $changeCategoryIdsArr);
        if(!$changeFunctionInfo = ChangeFunction::getInfo("change_category_ids in ({$changeCategoryIds}) and status='0'")) return false;
        if(empty($changeFunctionInfo['document_str'])) return false;
        return true;
    }

}